﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp.View.SystemInformation
{
    /// <summary>
    /// WD_UpdateStaffAccountManage.xaml 的交互逻辑
    /// </summary>
    public partial class WD_UpdateStaffAccountManage : Window
    {
        public DataRowView DRV;
        public WD_UpdateStaffAccountManage(DataRowView dr)
        {
            InitializeComponent();
            DRV = dr;
        }
        BLL.UCStaffAccountManage.UCStaffAccountManageClient myClient = new BLL.UCStaffAccountManage.UCStaffAccountManageClient();
        /// <summary>
        /// 页面加载（回填数据）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            #region 下拉框
            //权限组
            DataTable dtPermissionGroup = myClient.SelectPermissionGroup().Tables[0];
            cbo_Group.ItemsSource = dtPermissionGroup.DefaultView;
            cbo_Group.SelectedValuePath = "permissionGroupID";
            cbo_Group.DisplayMemberPath = "permissionGroupName";
            #endregion
            #region 数据回填
            txt_Account.Text = (DRV.Row["operatorAccounts"]).ToString();
            txt_Note.Text = (DRV.Row["note"]).ToString();
            PB_Password.Password = (DRV.Row["operatorPassword"]).ToString();
            txt_Name.Text = DRV.Row["staffName"].ToString().Trim();
            cbo_Group.SelectedValue = Convert.ToInt32(DRV.Row["permissionGroupID"]);
            if (DRV.Row["effective"].ToString() == "启用")
            {
                chk_Effect.IsChecked = true;
            }
            else
            {
                chk_Effect.IsChecked = false;

            }
            #endregion
        }
        /// <summary>
        /// 修改保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //(1)页面必填项
                if (txt_Name.Text != string.Empty && Convert.ToInt32(cbo_Group.SelectedValue) > 0 && txt_Account.Text.Trim() != string.Empty && PB_Password.Password.Trim() != string.Empty)
                {
                    //(2)获取数据
                    int intPGroupID = Convert.ToInt32(cbo_Group.SelectedValue);
                    int intStaffID = Convert.ToInt32(DRV.Row["staffID"]);//获取主页面表格选中行
                    string strAccounts = txt_Account.Text.Trim();
                    string strPassword = PB_Password.Password.Trim();
                    bool blEffective = (bool)chk_Effect.IsChecked;
                    string strNote = txt_Note.Text.Trim();
                    int intOperatorID = Convert.ToInt32(DRV.Row["operatorID"]);
                    //(3)执行修改
                    int intCount = myClient.UpdateOperator(intPGroupID, intStaffID, strAccounts, strPassword, blEffective, strNote, intOperatorID);
                    //（4）判断结果
                    if (intCount == -1)
                    {
                        MessageBox.Show("修改账号重复！", "系统提示", MessageBoxButton.OKCancel,
                          MessageBoxImage.Exclamation); //弹出确定对话框
                    }
                    else
                    {
                        MessageBoxResult dr = MessageBox.Show("修改账号成功！", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Asterisk); //弹出确定对话框                        
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("请把页面数据填写完整！", "系统提示", MessageBoxButton.OK,
                        MessageBoxImage.Warning); //弹出确定对话框
                }

            }
            catch (Exception)
            {
                MessageBox.Show("数据异常！", "系统提示", MessageBoxButton.OK,
                                         MessageBoxImage.Warning); //弹出确定对话框
            }
        }
        /// <summary>
        /// 取消窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult dr = MessageBox.Show("退出界面数据将不保留。", "系统提示", MessageBoxButton.OKCancel, MessageBoxImage.Information);//弹出确定对话框
            if (dr == MessageBoxResult.OK)//如果点了确定按钮
            {
                //关闭窗口
                this.Close();
            }
        }
    }
}
